package cc.ding.servlet;

import cc.ding.entity.Employee;
import cc.ding.service.Impl.PunchClockServiceImpl;
import cc.ding.service.PunchClockService;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * 打卡servlet
 */
@WebServlet(urlPatterns = "/punchClock.action")
public class punchClockServlet extends HttpServlet {
    PunchClockService punchClockService = new PunchClockServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();

        Employee employee = (Employee) session.getAttribute("employee");
        Map<String, String> map = punchClockService.punchClock(employee.getId());
        String jsonMap = JSON.toJSONString(map);
        resp.getWriter().print(jsonMap);
    }
}
